package com.css.fxfzysx.modules.vaolEngineering.service.impl;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.constants.YNEnum;
import com.css.fxfzysx.modules.cztDataMange.result.ParsingResult;
import com.css.fxfzysx.modules.lifeline.annotation.ValidationProcessor;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxBridgeEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxMainTableEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.repository.LifelineEngineeringRepository;
import com.css.fxfzysx.modules.lifeline.utils.ShpUtils;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.param.update.BridgeParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.CbVulnerabilityRepository;
import com.css.fxfzysx.modules.vaolEngineering.service.CbVulnerabilityService;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.modules.ysxManagement.entity.BdFactorEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.BdFactorRepository;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.ShpAnalyticUtil;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.zcpt.sys.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
@Slf4j
public class CbVulnerabilityServiceImpl implements CbVulnerabilityService {

    @Autowired
    private CbVulnerabilityRepository cbVulnerabilityRepository;

    @Autowired
    private BdFactorRepository bdFactorRepository;

    @Autowired
    private SUserService sUserService;

    @Autowired
    private ShpUtils shpUtils;

    @Autowired
    private MainTableUtils mainTableUtils;

    @Autowired
    private LifelineEngineeringRepository lifelineEngineeringRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse cobVulnerability(String versionId, String tableId,Integer bridgeType) {
        try {
            List<SmxBridgeEntity> bridgeList = new ArrayList<>();
            if (PlatformObjectUtils.isNotEmpty(tableId)) {
                SmxBridgeEntity entity = cbVulnerabilityRepository.getByTableId(tableId,bridgeType);
                bridgeList.add(entity);
            } else {
                bridgeList = cbVulnerabilityRepository.getByVersionId(versionId);
            }
            List<BdFactorEntity> allBdFactor = bdFactorRepository.getAllBdFactor();
            String[] fieldName = {"地震烈度", "场地土分类", "地基失效程度", "上部结构", "支座形式", "墩台高度", "墩台材料", "基础形式", "桥梁长度", "W0"};
            for (SmxBridgeEntity entity : bridgeList) {
                StringBuilder modelIds = new StringBuilder();
                HashMap<String, BigDecimal> dataMap = new HashMap<>();
                for (BdFactorEntity it : allBdFactor) {
                    if (it.getDamageFactor().equals("地震烈度")) {
                        if (mainTableUtils.getValueByKey("smxsfld",entity.getIntensity()).equals(it.getCategory())) {
                            dataMap.put("地震烈度", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("场地土分类")) {
                        if (mainTableUtils.getValueByKey("smxqlcdtfl",entity.getSiteSoil()).equals(it.getCategory())) {
                            dataMap.put("场地土分类", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("地基失效程度")) {
                        if (mainTableUtils.getValueByKey("smxqldjsx",entity.getFoundationFail()).equals(it.getCategory())) {
                            dataMap.put("地基失效程度", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("上部结构")) {
                        if (it.getCategory().contains(mainTableUtils.getValueByKey("smxqlsbjg",entity.getSuperStructure()))) {
                            dataMap.put("上部结构", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("支座形式")) {
                        if (mainTableUtils.getValueByKey("smxqlzzxs",entity.getBearing()).equals(it.getCategory())) {
                            dataMap.put("支座形式", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("墩台高度")) {
                        if (mainTableUtils.getValueByKey("smxqldtgd",entity.getPierHeight()).equals(it.getCategory())) {
                            dataMap.put("墩台高度", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("墩台材料")) {
                        if (mainTableUtils.getValueByKey("smxqlqdcl",entity.getPierMaterial()).equals(it.getCategory())) {
                            dataMap.put("墩台材料", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("基础形式")) {
                        if (it.getCategory().contains(mainTableUtils.getValueByKey("smxqljcxs",entity.getBasement()))) {
                            dataMap.put("基础形式", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("桥梁长度")) {
                        if (mainTableUtils.getValueByKey("smxqlkcykd",entity.getNumber()).equals(it.getCategory())) {
                            dataMap.put("桥梁长度", it.getCoefficientValue());
                            modelIds.append(it.getCbdfId()).append(",");
                        }
                    }
                    if (it.getDamageFactor().equals("W0")) {
                        dataMap.put("W0", it.getCoefficientValue());
                        modelIds.append(it.getCbdfId());
                    }
                }
                //校验参数值
                if (!verificationParameters(dataMap, fieldName).equals("ok")) {
                    return RestResponse.fail(verificationParameters(dataMap, fieldName));
                }
                Collection<BigDecimal> values = dataMap.values();
                BigDecimal reduce = values.stream().reduce(new BigDecimal(1), BigDecimal::multiply).setScale(2, RoundingMode.HALF_UP);
                entity.setCalculationResults(reduce);
                if (PlatformObjectUtils.isEmpty(entity.getCalculationResultsCorrect())) {
                    entity.setCalculationResultsCorrect(reduce);
                }
                if (reduce.compareTo(BigDecimal.ZERO) != -1 && reduce.compareTo(new BigDecimal(1.22)) < 0) {
                    entity.setDamage("基本完好");
                }
                if (reduce.compareTo(BigDecimal.valueOf(1.23)) != -1 && reduce.compareTo(BigDecimal.valueOf(2.19)) < 0) {
                    entity.setDamage("轻微破坏");
                }
                if (reduce.compareTo(BigDecimal.valueOf(2.20)) != -1 && reduce.compareTo(BigDecimal.valueOf(3.37)) < 0) {
                    entity.setDamage("中等破坏");
                }
                if (reduce.compareTo(BigDecimal.valueOf(3.38)) != -1 && reduce.compareTo(BigDecimal.valueOf(4.39)) < 0) {
                    entity.setDamage("严重破坏");
                }
                if (reduce.compareTo(BigDecimal.valueOf(4.39)) > 0) {
                    entity.setDamage("毁坏");
                }

                if (PlatformObjectUtils.isNotEmpty(entity.getCalculationResultsCorrect())) {
                    BigDecimal aiCorrect = entity.getCalculationResultsCorrect();
                    if (aiCorrect.compareTo(BigDecimal.ZERO) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(1.22)) < 0) {
                        entity.setDamageCorrect("基本完好");
                    }
                    if (aiCorrect.compareTo(BigDecimal.valueOf(1.23)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(2.19)) < 0) {
                        entity.setDamageCorrect("轻微破坏");
                    }
                    if (aiCorrect.compareTo(BigDecimal.valueOf(2.20)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(3.37)) < 0) {
                        entity.setDamageCorrect("中等破坏");
                    }
                    if (aiCorrect.compareTo(BigDecimal.valueOf(3.38)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(4.39)) < 0) {
                        entity.setDamageCorrect("严重破坏");
                    }
                    if (aiCorrect.compareTo(BigDecimal.valueOf(4.39)) > 0) {
                        entity.setDamageCorrect("毁坏");
                    }
                }
                cbVulnerabilityRepository.saveBridgeResult(entity,bridgeType);
                mainTableUtils.updateModelBaseIds(modelIds.toString(), null, entity.getTableId());
                String year = mainTableUtils.getKeyByValue("smxjsnd", MainTableUtils.getYear(entity.getYear()));
                DestructiveStateVo stateVo = cbVulnerabilityRepository.getByBasicInfo(entity,year);
                switch (entity.getIntensity()){
                    case "Ⅵ":
                        stateVo.setDisasterRate6State(entity.getDamage());
                    case "Ⅶ":
                        stateVo.setDisasterRate6State(entity.getDamage());
                    case "Ⅷ":
                        stateVo.setDisasterRate6State(entity.getDamage());
                    case "Ⅸ":
                        stateVo.setDisasterRate6State(entity.getDamage());
                    case "Ⅹ":
                        stateVo.setDisasterRate6State(entity.getDamage());
                }
                mainTableUtils.updateCalculationStatus(entity.getTableId(),stateVo.getDisasterRate6State(),stateVo.getDisasterRate7State(),stateVo.getDisasterRate8State(),stateVo.getDisasterRate9State(),stateVo.getDisasterRate10State(), 1);
            }
            mainTableUtils.updateVersionCalculationStatus(versionId, 0);
            return RestResponse.succeed("计算成功!");
        } catch (Exception e) {
            log.error("get cobVulnerability error:{}", e.getMessage(), e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    private static String verificationParameters(HashMap<String, BigDecimal> dataMap, String[] fieldNames) {
        String message = "";
        for (int i = 0; i < fieldNames.length; i++) {
            if (!dataMap.containsKey(fieldNames[i])) {
                return message = "失败,请在 【桥梁震害因子量化取值表】 中维护 {" + fieldNames[i] + "} 信息";
            }
        }
        return message = "ok";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> uploadBridge(String fileIds, String versionId, String userId,Integer bridgeType) throws Exception {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxBridgeEntity> bridgeList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            String id = UUIDGenerator.getUUID();
            List<Object> list = shpList.get(i);
            if (list.size() < 25) {
                return null;
            }
            SmxBridgeEntity bridgeEntity = new SmxBridgeEntity();
            bridgeEntity.setDelFlag(YNEnum.N.toString());
            bridgeEntity.setId(UUIDGenerator.getUUID());
            bridgeEntity.setTableId(id);
            bridgeEntity.setCreateUser(userId);
            bridgeEntity.setCreateTime(PlatformDateUtils.getCurrentDate());

            if (PlatformObjectUtils.isNotEmpty(list.get(0))) {
                bridgeEntity.setGeom(list.get(0).toString());
            }
            if (!bridgeEntity.getGeom().contains("POINT") && bridgeType == 0) {
                map.put("res", false);
                map.put("mess", "失败,请上传点数据!");
                return map;
            }

            if (bridgeEntity.getGeom().contains("POINT") && bridgeType == 1) {
                map.put("res", false);
                map.put("mess", "失败,请上传线数据!");
                return map;
            }

            if (PlatformObjectUtils.isNotEmpty(list.get(1))) {
                bridgeEntity.setBridgeId(list.get(1).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(2))) {
                bridgeEntity.setName(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "桥梁名称", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(3))) {
                bridgeEntity.setLongitude(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "经度", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(4))) {
                bridgeEntity.setLatitude(ShpAnalyticUtil.getStringVal(list.get(4).toString(), "纬度", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(5))) {
                bridgeEntity.setAcrossObject(ShpAnalyticUtil.getStringVal(list.get(5).toString(), "跨越地物名称", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(6))) {
                bridgeEntity.setIntensity(ShpAnalyticUtil.getStringVal(list.get(6).toString(), "抗震设防烈度", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(7))) {
                bridgeEntity.setYear(ShpAnalyticUtil.getIntegerVal(list.get(7), "建造年代", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(8))) {
                bridgeEntity.setFortification(ShpAnalyticUtil.getStringVal(list.get(8).toString(), "设防情况", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(9))) {
                bridgeEntity.setStructure(ShpAnalyticUtil.getStringVal(list.get(9).toString(), "桥梁结构类型", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(10))) {
                bridgeEntity.setMaxSpan(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(10).toString(), "最大跨经", true)));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(11))) {
                bridgeEntity.setLength(BigDecimal.valueOf(ShpAnalyticUtil.getDoubleVal(list.get(11).toString(), "跨经总长", true)));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(12))) {
                bridgeEntity.setSiteSoil(ShpAnalyticUtil.getStringVal(list.get(12).toString(), "场地土分类", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(13))) {
                bridgeEntity.setGrade(ShpAnalyticUtil.getStringVal(list.get(13).toString(), "桥梁等级", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(14))) {
                bridgeEntity.setFoundationFail(ShpAnalyticUtil.getStringVal(list.get(14).toString(), "地基失效程度", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(15))) {
                bridgeEntity.setSuperStructure(ShpAnalyticUtil.getStringVal(list.get(15).toString(), "上部结构", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(16))) {
                bridgeEntity.setBearing(ShpAnalyticUtil.getStringVal(list.get(16).toString(), "上部结构", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(17))) {
                bridgeEntity.setPierHeight(getPierHeight(ShpAnalyticUtil.getDoubleVal(list.get(17).toString(), "", false)));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(18))) {
                bridgeEntity.setPierMaterial(list.get(18).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(19))) {
                bridgeEntity.setBasement(list.get(19).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(20))) {
                bridgeEntity.setNumber(list.get(20).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(21))) {
                bridgeEntity.setCapacity(new BigDecimal(list.get(21).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(22))) {
                bridgeEntity.setAbutmentMat(list.get(22).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(23))) {
                bridgeEntity.setAbutmentType(list.get(23).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(24))) {
                bridgeEntity.setExpansionJoint(new BigDecimal(list.get(24).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(25))) {
                bridgeEntity.setWidth(new BigDecimal(list.get(25).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(26))) {
                bridgeEntity.setNote(list.get(26).toString());
            }
            if (list.size() > 27 && !PlatformObjectUtils.isEmpty(list.get(27))) {
                bridgeEntity.setExtends1(list.get(27).toString());
            }
            if (list.size() > 28 && !PlatformObjectUtils.isEmpty(list.get(28))) {
                bridgeEntity.setExtends2(list.get(28).toString());
            }
            if (list.size() > 29 && !PlatformObjectUtils.isEmpty(list.get(29))) {
                bridgeEntity.setExtends3(list.get(29).toString());
            }
            if (list.size() > 30 && !PlatformObjectUtils.isEmpty(list.get(30))) {
                bridgeEntity.setExtends4(list.get(30).toString());
            }
            if (list.size() > 31 && !PlatformObjectUtils.isEmpty(list.get(31))) {
                bridgeEntity.setExtends5(list.get(31).toString());
            }
            if (list.size() > 32 && !PlatformObjectUtils.isEmpty(list.get(32))) {
                bridgeEntity.setExtends6(list.get(32).toString());
            }
            if (list.size() > 33 && !PlatformObjectUtils.isEmpty(list.get(33))) {
                bridgeEntity.setExtends7(list.get(33).toString());
            }
            if (list.size() > 34 && !PlatformObjectUtils.isEmpty(list.get(34))) {
                bridgeEntity.setExtends8(list.get(34).toString());
            }
            if (list.size() > 35 && !PlatformObjectUtils.isEmpty(list.get(35))) {
                bridgeEntity.setExtends9(list.get(35).toString());
            }
            if (list.size() > 36 && !PlatformObjectUtils.isEmpty(list.get(36))) {
                bridgeEntity.setExtends10(list.get(36).toString());
            }

            bridgeEntity.setVersionId(versionId);
            bridgeList.add(bridgeEntity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setCreateUser(userId);
            mainEntity.setYear(bridgeEntity.getYear());
            mainEntity.setVersionId(versionId);
            mainEntity.setFortification(bridgeEntity.getFortification());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_QL);
            mainTableUtils.saveMainTable(mainEntity);
        }
        boolean res = false;

        int i = 1;
        if(bridgeType == 0){
            for (SmxBridgeEntity entity : bridgeList) {
                entity.setSerialNumber(i);
                res = cbVulnerabilityRepository.saveBridgePoint(entity);
                i += 1;
            }
        }else {
            for (SmxBridgeEntity entity : bridgeList) {
                entity.setSerialNumber(i);
                res = cbVulnerabilityRepository.saveBridgeLine(entity);
                i += 1;
            }
        }
        map.put("res", res);
        return map;
    }

    private String getPierHeight(Double pierHeight) {
        if (pierHeight < 5) {
            return "1";
        }
        if (pierHeight >= 5 && pierHeight <= 10) {
            return "2";
        }
        if (pierHeight > 10) {
            return "3";
        }
        return "";
    }

    @Override
    public HashMap<Integer, ArrayList<String>> getDataValidation(String versionId) {
        HashMap<Integer, ArrayList<String>> map = new HashMap<>();
        SmxVersionEntity versionEntity = lifelineEngineeringRepository.getById(versionId);
        if (PlatformObjectUtils.isNotEmpty(versionEntity)) {
            List<SmxBridgeEntity> roadList = cbVulnerabilityRepository.getByVersionId(versionEntity.getId());
            for (SmxBridgeEntity bridgeEntity : roadList) {
                ArrayList<String> list = ValidationProcessor.validate(bridgeEntity);
                if (list.size() != 0) {
                    map.put(bridgeEntity.getSerialNumber(), list);
                }
            }
        }
        return map;
    }

    @Override
    public SmxBridgeEntity getByTableId(String id,Integer bridgeType) {
        return cbVulnerabilityRepository.getByTableId(id,bridgeType);
    }

    @Override
    public void updateBridgeById(BridgeParam bridgeParam,Integer bridgeType) {
        cbVulnerabilityRepository.updateBridgeById(bridgeParam,bridgeType);
    }

    @Override
    public RestResponse count(BridgeParam param) {
        List<BdFactorEntity> allBdFactor = bdFactorRepository.getAllBdFactor();
        StringBuilder modelIds = new StringBuilder();
        String[] fieldName = {"地震烈度", "场地土分类", "地基失效程度", "上部结构", "支座形式", "墩台高度", "墩台材料", "基础形式", "桥梁长度", "W0"};
        HashMap<String, BigDecimal> dataMap = new HashMap<>();
        for (BdFactorEntity it : allBdFactor) {
            if (it.getDamageFactor().equals("地震烈度")) {
                if (mainTableUtils.getValueByKey("smxsfld",param.getIntensity()).equals(it.getCategory())) {
                    dataMap.put("地震烈度", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("场地土分类")) {
                if (mainTableUtils.getValueByKey("smxqlcdtfl",param.getSiteSoil()).equals(it.getCategory())) {
                    dataMap.put("场地土分类", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("地基失效程度")) {
                if (mainTableUtils.getValueByKey("smxqldjsx",param.getFoundationFail()).equals(it.getCategory())) {
                    dataMap.put("地基失效程度", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("上部结构")) {
                if (it.getCategory().contains(mainTableUtils.getValueByKey("smxqlsbjg",param.getSuperStructure()))) {
                    dataMap.put("上部结构", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("支座形式")) {
                if (mainTableUtils.getValueByKey("smxqlzzxs",param.getBearing()).equals(it.getCategory())) {
                    dataMap.put("支座形式", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("墩台高度")) {
                if (mainTableUtils.getValueByKey("smxqldtgd",param.getPierHeight()).equals(it.getCategory())) {
                    dataMap.put("墩台高度", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("墩台材料")) {
                if (mainTableUtils.getValueByKey("smxqlqdcl",param.getPierMaterial()).equals(it.getCategory())) {
                    dataMap.put("墩台材料", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("基础形式")) {
                if (it.getCategory().contains(mainTableUtils.getValueByKey("smxqljcxs",param.getBasement()))) {
                    dataMap.put("基础形式", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("桥梁长度")) {
                if (mainTableUtils.getValueByKey("smxqlkcykd",param.getNumber()).equals(it.getCategory())) {
                    dataMap.put("桥梁长度", it.getCoefficientValue());
                    modelIds.append(it.getCbdfId()).append(",");
                }
            }
            if (it.getDamageFactor().equals("W0")) {
                dataMap.put("W0", it.getCoefficientValue());
                modelIds.append(it.getCbdfId());
            }
        }
        //校验参数值
        if (!verificationParameters(dataMap, fieldName).equals("ok")) {
            return RestResponse.fail(verificationParameters(dataMap, fieldName));
        }
        Collection<BigDecimal> values = dataMap.values();
        BigDecimal reduce = values.stream().reduce(new BigDecimal(1), BigDecimal::multiply).setScale(2, RoundingMode.HALF_UP);
        param.setCalculationResults(reduce);
        if (PlatformObjectUtils.isEmpty(param.getCalculationResultsCorrect())) {
            param.setCalculationResultsCorrect(reduce);
        }
        if (reduce.compareTo(BigDecimal.ZERO) != -1 && reduce.compareTo(new BigDecimal(1.22)) < 0) {
            param.setDamage("基本完好");
        }
        if (reduce.compareTo(BigDecimal.valueOf(1.23)) != -1 && reduce.compareTo(BigDecimal.valueOf(2.19)) < 0) {
            param.setDamage("轻微破坏");
        }
        if (reduce.compareTo(BigDecimal.valueOf(2.20)) != -1 && reduce.compareTo(BigDecimal.valueOf(3.37)) < 0) {
            param.setDamage("中等破坏");
        }
        if (reduce.compareTo(BigDecimal.valueOf(3.38)) != -1 && reduce.compareTo(BigDecimal.valueOf(4.39)) < 0) {
            param.setDamage("严重破坏");
        }
        if (reduce.compareTo(BigDecimal.valueOf(4.39)) > 0) {
            param.setDamage("毁坏");
        }

        if (PlatformObjectUtils.isNotEmpty(param.getCalculationResultsCorrect())) {
            BigDecimal aiCorrect = param.getCalculationResultsCorrect();
            if (aiCorrect.compareTo(BigDecimal.ZERO) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(1.22)) < 0) {
                param.setDamageCorrect("基本完好");
            }
            if (aiCorrect.compareTo(BigDecimal.valueOf(1.23)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(2.19)) < 0) {
                param.setDamageCorrect("轻微破坏");
            }
            if (aiCorrect.compareTo(BigDecimal.valueOf(2.20)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(3.37)) < 0) {
                param.setDamageCorrect("中等破坏");
            }
            if (aiCorrect.compareTo(BigDecimal.valueOf(3.38)) != -1 && aiCorrect.compareTo(BigDecimal.valueOf(4.39)) < 0) {
                param.setDamageCorrect("严重破坏");
            }
            if (aiCorrect.compareTo(BigDecimal.valueOf(4.39)) > 0) {
                param.setDamageCorrect("毁坏");
            }
        }
        param.setModelIds(modelIds.toString());
        return RestResponse.succeed(param);
    }
}
